//index.js
//获取应用实例
const app = getApp()
const httpUtils = require('../../utils/httpUtils.js');
Page({
  data: {
    imageurl: 'https://material.sse100.cn/shop/',
    barInfo: null,
    banner: null,
    longitude: null,
    latitude: null,
    courses: null,
    categray: null,
    currentIndex: "0",
    // title: ["首页","掘金","思否","知乎"],
    left: "",
    // flag: true,
    guess: null, //猜你喜欢
    listData: null,
    type: 0, //热门课程类型 0小学1初中2高中3大学
    package: [], //热门课程
    packageCurrent: null,
    teachingAids: null, //教辅的资料
    // referer: null, //分享人信息
    // org: null, //门店名称
  },
  changeTab: function (e) {
    //console.log(e)
    this.setData({
      currentIndex: e.currentTarget.dataset.aa,
      packageCurrent: this.data.package[e.currentTarget.dataset.aa]
    })
    this.changeline(e)

  },
  changeline: function () {
    const query = wx.createSelectorQuery()
    var _this = this
    query.select('.tabTrue').boundingClientRect()
    query.exec(function (res) {
      _this.setData({
        left: res[0].left
      })
      //console.log(res[0].left)
    })
  },
  onLoad: function () {
    let that = this;

    this.setData({
      barInfo: app.globalData.barInfo
    });
    
    that.getData()
  },

  onShow: function () {
    this.getData(); // 每次显示页面都重新获取最新数据
  },

  getData: function () {
    let that = this
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    httpUtils.index({
      access_token: app.globalData.$TOKEN
    }).then((res) => {
      wx.hideLoading()
      console.log("res.data:" + res.data.referer)
      if (res.data.referer && res.data.referer.username) { //联系人信息不为空弹框
        // that.setData({
        //   flag: false,
        //   referee: res.data.referer,
        //   org: res.data.position.org
        // })
        app.globalData.referee = res.data.referer

        // console.log("app.globalData.referee:" + app.globalData.referee.username)
      }
      that.setData({
        banner: res.data.carousel,
        // courses: res.data.courses,
        barInfo: app.globalData.barInfo,
        categray: res.data.course.categray,
        package: res.data.course.package,
        guess: res.data.guess,
        teachingAids: res.data.teaching_aids,
        packageCurrent: res.data.course.package[0]

      });
      console.log("res.data.course.package[0]:" + res.data.course.package[0][0].id)
      this.changeline(1)
      // that.initData(res.data)
    });


  },
  initData: function (data) {
    let listData = new Array();
    (data.course_type).forEach(function (item, index) {
      console.log("data.courses:" + item.title);
      if (item.id == 0) {
        if (data.courses) {
          let itemData = {
            id: item.id,
            name: item.title,
            courses: data.courses
          }
          listData.push(itemData)
        }

      } else if (item.id == 1) {

      } else if (item.id == 2) {

      } else if (item.id == 3) {
        if (data.j_courses) {
          let itemData = {
            id: item.id,
            name: item.title,
            courses: data.j_courses
          }
          listData.push(itemData)
        }

      } else {
        if (data.s_courses) {
          let itemData = {
            id: item.id,
            name: item.title,
            courses: data.s_courses
          }
          listData.push(itemData)
        }
      }
    })
    this.setData({
      listData: listData
    })
    listData.forEach(function (item, index) {
      console.log("item.id:" + item.id + "item.title:" + item.name);
    })
  },
  
  lookdetail: function (e) {
    var currext = e.currentTarget.dataset['id'];
    console.log("currext:" + currext)
    //查看详情
    wx.navigateTo({
      url: '/pages/coreseDetail/coreseDetail?id=' + currext,
    })
  },
  bingMore: function (e) {
    //查看更多
    var currext = e.currentTarget.dataset['id'];
    wx.navigateTo({
      url: '/pages/courselist/courselist?id=' + currext,
    })
  },
  /**
   * 教辅点击更多
   */
  bingTeachMore: function () {
    wx.navigateTo({
      url: '/pages/teachTools/teachTools',
    })
  },
    /** 
   * 精选课程点击更多
  */
 bingCourseMore:function(e){
    var currext = e.currentTarget.dataset['data'];
	wx.setStorageSync(
		'course_type',currext
	)
    wx.switchTab({
      url: '../allCourseList/allCourseList',
    })
  },
  /** 
   * 精选课程点击课程查看详情
  */
  lookCoursedetail:function (e) {
    var currext = e.currentTarget.dataset['id'];
    console.log("currext:" + currext)
    //查看详情
    wx.navigateTo({
      url: '/pages/coreseDetail/coreseDetail?id=' + currext,
    })
  },
})